﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Net;

//Simple and stupid Abyss Webserver password changer and restarter
namespace AbyssSetup
{
    class Program
    {
        public static string ReadPassword() {
            Stack<string> passbits = new Stack<string>();
            for (ConsoleKeyInfo cki = Console.ReadKey(true); cki.Key != ConsoleKey.Enter; cki = Console.ReadKey(true)) {
              if (cki.Key == ConsoleKey.Backspace) {
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                Console.Write(" ");
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                passbits.Pop();
              }
              else {
                Console.Write("*");
                passbits.Push(cki.KeyChar.ToString());
              }
            }
            string[] pass = passbits.ToArray();
            Array.Reverse(pass);
            return string.Join(string.Empty, pass);
        }
        static void Main(string[] args)
        {
            int i = 0;
            string path = Environment.CurrentDirectory + "\\";

            //Kill any running server process
            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName == "abyssws")
                {
                    clsProcess.Kill();
                    Console.WriteLine("Sulgen serveri protsessi " + i);
                    i++;
                }
            }
            i = 0;

            //User input
            string user = "root";
            string password = "admin";
            Console.ForegroundColor = ConsoleColor.Red;
            Console.InputEncoding = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;
            Console.WriteLine("Seadistamiseks vajuta ENTER. NB! Eelmine seadistus kirjutatakse yle!");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.ReadLine();
            Console.WriteLine("Serveri ja Administratsiooni kasutajanimi (tyhjaks jattes on \"root\") :");
            string temp1 = Console.ReadLine();
            if (temp1 != "")
            {
                user = temp1;
            }
            restart:
            Console.WriteLine("Parool (tyhjaks jattes on \"admin\") :");
            string temp2 = ReadPassword();
            Console.WriteLine("\nParool uuesti:");
            string temp3 = ReadPassword();
            if (temp3 != "" && temp2 != "")
            {
                if (temp2 == temp3)
                {
                    password = temp2;
                }
                else
                {
                    Console.WriteLine("\nParoolid ei klapi! Sisesta uuesti!\n");
                    goto restart;
                }
            }

            //Change config parameters and save
            #region abyss.conf file string
            string conf = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<root>
	<server>
		<parameters>
			<root>
				.
			</root>
			<timeout>
				30
			</timeout>
			<maxconn>
				20
			</maxconn>
			<keepalive>
				10
			</keepalive>
			<opsys>
				<pidfile>
					log/abyssws.pid
				</pidfile>
			</opsys>
		</parameters>
		<host>
			<names/>
			<path>
				htdocs/
			</path>
			<running>
				yes
			</running>
			<id>
				1
			</id>
			<scripting>
				<enabled>
					yes
				</enabled>
				<cgi>
					<timeout>
						30
					</timeout>
					<useregistry>
						no
					</useregistry>
					<useshebang>
						yes
					</useshebang>
					<errorfile>
						log/cgi.log
					</errorfile>
				</cgi>
				<isapi>
					<ext>
						dll
					</ext>
					<errorfile>
						log/isapi.log
					</errorfile>
					<debuglevel>
						0
					</debuglevel>
				</isapi>
				<fastcgi>
					<errorfile>
						log/fastcgi.log
					</errorfile>
					<timeout>
						240
					</timeout>
					<debuglevel>
						1
					</debuglevel>
				</fastcgi>
				<interpreters>
					<interpreter>
						<ext>
							php
						</ext>
						<interface>
							1
						</interface>
						<file>
							PHP5\php-cgi.exe
						</file>
						<checkexists>
							yes
						</checkexists>
						<type>
							1
						</type>
						<updatepaths>
							yes
						</updatepaths>
					</interpreter>
				</interpreters>
				<environment>
					<var>
						<name>
							REDIRECT_STATUS
						</name>
						<value>
							200
						</value>
					</var>
				</environment>
			</scripting>
			<ssi>
				<extensions>
					<ext>
						shtml
					</ext>
					<ext>
						shtm
					</ext>
					<ext>
						stm
					</ext>
				</extensions>
				<enabled>
					yes
				</enabled>
				<errormessage/>
				<timeformat/>
				<abbreviatesize>
					yes
				</abbreviatesize>
				<execcmd>
					no
				</execcmd>
			</ssi>
			<log>
				<file>
					log/access.log
				</file>
				<extendedformat>
					yes
				</extendedformat>
				<denied>
					<path>
						/admin
					</path>
					<ip>
						127.0.0.1
					</ip>
				</denied>
			</log>
			<indexes>
				<index>
					index.html
				</index>
				<index>
					index.php
				</index>
				<index>
					index.htm
				</index>
				<index>
					default.aspx
				</index>
			</indexes>
			<antileech>
				<paths/>
				<redirect/>
				<strict>
					no
				</strict>
				<allowed/>
			</antileech>
			<bindip>
				*
			</bindip>
			<dirlist>
				<type>
					0
				</type>
				<scope>
					<order>
						ad
					</order>
					<allow>
						<path>
							/
						</path>
					</allow>
				</scope>
				<hidden>
					<file>
						.*
					</file>
				</hidden>
			</dirlist>
			<port>
				80
			</port>
			<compress>
				<level>
					6
				</level>
				<scope>
					<allow>
						<path>
							/
						</path>
					</allow>
					<order>
						ad
					</order>
				</scope>
				<mimes>
					<allow>
						<mime>
							text/*
						</mime>
					</allow>
				</mimes>
			</compress>
			<ac>
				<rules>
					<rule>
						<path>
							/admin
						</path>
						<realm>
							Owl - Administratsioon
						</realm>
						<order>
							ad
						</order>
						<allow>
							<user>
								" + user + @"
							</user>
						</allow>
					</rule>
				</rules>
			</ac>
			<users>
				<user>
					<name>
						" + user + @"
					</name>
					<password/>
				</user>
			</users>
		</host>
		<throttle>
			<maxspeed/>
			<maxspeedperip/>
		</throttle>
		<antihack>
			<enabled>
				no
			</enabled>
			<threshold>
				20
			</threshold>
			<monitorperiod>
				20
			</monitorperiod>
			<banperiod>
				60
			</banperiod>
			<logfile/>
			<denied>
				<ip>
					127.0.0.1
				</ip>
			</denied>
		</antihack>
		<mimes>
			<mime>
				<type>
					application/mac-binhex40
				</type>
				<ext>
					hqx
				</ext>
			</mime>
			<mime>
				<type>
					application/msword
				</type>
				<ext>
					doc
				</ext>
			</mime>
			<mime>
				<type>
					application/octet-stream
				</type>
				<ext>
					bin
				</ext>
				<ext>
					dms
				</ext>
				<ext>
					lha
				</ext>
				<ext>
					lzh
				</ext>
				<ext>
					exe
				</ext>
				<ext>
					class
				</ext>
			</mime>
			<mime>
				<type>
					application/pdf
				</type>
				<ext>
					pdf
				</ext>
			</mime>
			<mime>
				<type>
					application/postscript
				</type>
				<ext>
					ai
				</ext>
				<ext>
					eps
				</ext>
				<ext>
					ps
				</ext>
			</mime>
			<mime>
				<type>
					application/smil
				</type>
				<ext>
					smi
				</ext>
				<ext>
					smil
				</ext>
			</mime>
			<mime>
				<type>
					application/vnd.mif
				</type>
				<ext>
					mif
				</ext>
			</mime>
			<mime>
				<type>
					application/vnd.ms-asf
				</type>
				<ext>
					asf
				</ext>
			</mime>
			<mime>
				<type>
					application/vnd.ms-excel
				</type>
				<ext>
					xls
				</ext>
			</mime>
			<mime>
				<type>
					application/vnd.ms-powerpoint
				</type>
				<ext>
					ppt
				</ext>
			</mime>
			<mime>
				<type>
					application/x-cdlink
				</type>
				<ext>
					vcd
				</ext>
			</mime>
			<mime>
				<type>
					application/x-compress
				</type>
				<ext>
					Z
				</ext>
			</mime>
			<mime>
				<type>
					application/x-cpio
				</type>
				<ext>
					cpio
				</ext>
			</mime>
			<mime>
				<type>
					application/x-csh
				</type>
				<ext>
					csh
				</ext>
			</mime>
			<mime>
				<type>
					application/x-director
				</type>
				<ext>
					dcr
				</ext>
				<ext>
					dir
				</ext>
				<ext>
					dxr
				</ext>
			</mime>
			<mime>
				<type>
					application/x-dvi
				</type>
				<ext>
					dvi
				</ext>
			</mime>
			<mime>
				<type>
					application/x-gtar
				</type>
				<ext>
					gtar
				</ext>
			</mime>
			<mime>
				<type>
					application/x-gzip
				</type>
				<ext>
					gz
				</ext>
			</mime>
			<mime>
				<type>
					application/x-javascript
				</type>
				<ext>
					js
				</ext>
			</mime>
			<mime>
				<type>
					application/x-latex
				</type>
				<ext>
					latex
				</ext>
			</mime>
			<mime>
				<type>
					application/x-sh
				</type>
				<ext>
					sh
				</ext>
			</mime>
			<mime>
				<type>
					application/x-shar
				</type>
				<ext>
					shar
				</ext>
			</mime>
			<mime>
				<type>
					application/x-shockwave-flash
				</type>
				<ext>
					swf
				</ext>
			</mime>
			<mime>
				<type>
					application/x-stuffit
				</type>
				<ext>
					sit
				</ext>
			</mime>
			<mime>
				<type>
					application/x-tar
				</type>
				<ext>
					tar
				</ext>
			</mime>
			<mime>
				<type>
					application/x-tcl
				</type>
				<ext>
					tcl
				</ext>
			</mime>
			<mime>
				<type>
					application/x-tex
				</type>
				<ext>
					tex
				</ext>
			</mime>
			<mime>
				<type>
					application/x-texinfo
				</type>
				<ext>
					texinfo
				</ext>
				<ext>
					texi
				</ext>
			</mime>
			<mime>
				<type>
					application/x-troff
				</type>
				<ext>
					t
				</ext>
				<ext>
					tr
				</ext>
				<ext>
					roff
				</ext>
			</mime>
			<mime>
				<type>
					application/x-troff-man
				</type>
				<ext>
					man
				</ext>
			</mime>
			<mime>
				<type>
					application/x-troff-me
				</type>
				<ext>
					me
				</ext>
			</mime>
			<mime>
				<type>
					application/x-troff-ms
				</type>
				<ext>
					ms
				</ext>
			</mime>
			<mime>
				<type>
					application/zip
				</type>
				<ext>
					zip
				</ext>
			</mime>
			<mime>
				<type>
					application/vnd.wap.wmlc
				</type>
				<ext>
					wmlc
				</ext>
			</mime>
			<mime>
				<type>
					audio/basic
				</type>
				<ext>
					au
				</ext>
				<ext>
					snd
				</ext>
			</mime>
			<mime>
				<type>
					audio/midi
				</type>
				<ext>
					mid
				</ext>
				<ext>
					midi
				</ext>
				<ext>
					kar
				</ext>
			</mime>
			<mime>
				<type>
					audio/mpeg
				</type>
				<ext>
					mpga
				</ext>
				<ext>
					mp2
				</ext>
				<ext>
					mp3
				</ext>
			</mime>
			<mime>
				<type>
					audio/x-aiff
				</type>
				<ext>
					aif
				</ext>
				<ext>
					aiff
				</ext>
				<ext>
					aifc
				</ext>
			</mime>
			<mime>
				<type>
					audio/x-pn-realaudio
				</type>
				<ext>
					ram
				</ext>
				<ext>
					rm
				</ext>
			</mime>
			<mime>
				<type>
					audio/x-realaudio
				</type>
				<ext>
					ra
				</ext>
			</mime>
			<mime>
				<type>
					audio/x-wav
				</type>
				<ext>
					wav
				</ext>
			</mime>
			<mime>
				<type>
					image/bmp
				</type>
				<ext>
					bmp
				</ext>
			</mime>
			<mime>
				<type>
					image/gif
				</type>
				<ext>
					gif
				</ext>
			</mime>
			<mime>
				<type>
					image/ief
				</type>
				<ext>
					ief
				</ext>
			</mime>
			<mime>
				<type>
					image/jpeg
				</type>
				<ext>
					jpeg
				</ext>
				<ext>
					jpg
				</ext>
				<ext>
					jpe
				</ext>
			</mime>
			<mime>
				<type>
					image/png
				</type>
				<ext>
					png
				</ext>
			</mime>
			<mime>
				<type>
					image/tiff
				</type>
				<ext>
					tiff
				</ext>
				<ext>
					tif
				</ext>
			</mime>
			<mime>
				<type>
					image/vnd.wap.wbmp
				</type>
				<ext>
					wbmp
				</ext>
			</mime>
			<mime>
				<type>
					image/x-cmu-raster
				</type>
				<ext>
					ras
				</ext>
			</mime>
			<mime>
				<type>
					image/x-portable-anymap
				</type>
				<ext>
					pnm
				</ext>
			</mime>
			<mime>
				<type>
					image/x-portable-bitmap
				</type>
				<ext>
					pbm
				</ext>
			</mime>
			<mime>
				<type>
					image/x-portable-graymap
				</type>
				<ext>
					pgm
				</ext>
			</mime>
			<mime>
				<type>
					image/x-portable-pixmap
				</type>
				<ext>
					ppm
				</ext>
			</mime>
			<mime>
				<type>
					image/x-rgb
				</type>
				<ext>
					rgb
				</ext>
			</mime>
			<mime>
				<type>
					image/x-xbitmap
				</type>
				<ext>
					xbm
				</ext>
			</mime>
			<mime>
				<type>
					image/x-xpixmap
				</type>
				<ext>
					xpm
				</ext>
			</mime>
			<mime>
				<type>
					image/x-xwindowdump
				</type>
				<ext>
					xwd
				</ext>
			</mime>
			<mime>
				<type>
					image/x-icon
				</type>
				<ext>
					ico
				</ext>
			</mime>
			<mime>
				<type>
					model/iges
				</type>
				<ext>
					igs
				</ext>
				<ext>
					iges
				</ext>
			</mime>
			<mime>
				<type>
					model/mesh
				</type>
				<ext>
					msh
				</ext>
				<ext>
					mesh
				</ext>
				<ext>
					silo
				</ext>
			</mime>
			<mime>
				<type>
					model/vrml
				</type>
				<ext>
					wrl
				</ext>
				<ext>
					vrml
				</ext>
			</mime>
			<mime>
				<type>
					text/css
				</type>
				<ext>
					css
				</ext>
			</mime>
			<mime>
				<type>
					text/html
				</type>
				<ext>
					html
				</ext>
				<ext>
					htm
				</ext>
			</mime>
			<mime>
				<type>
					text/plain
				</type>
				<ext>
					asc
				</ext>
				<ext>
					txt
				</ext>
			</mime>
			<mime>
				<type>
					text/richtext
				</type>
				<ext>
					rtx
				</ext>
			</mime>
			<mime>
				<type>
					text/rtf
				</type>
				<ext>
					rtf
				</ext>
			</mime>
			<mime>
				<type>
					text/sgml
				</type>
				<ext>
					sgml
				</ext>
				<ext>
					sgm
				</ext>
			</mime>
			<mime>
				<type>
					text/tab-separated-values
				</type>
				<ext>
					tsv
				</ext>
			</mime>
			<mime>
				<type>
					text/xml
				</type>
				<ext>
					xml
				</ext>
			</mime>
			<mime>
				<type>
					text/vnd.wap.wml
				</type>
				<ext>
					wml
				</ext>
			</mime>
			<mime>
				<type>
					text/vnd.wap.wmlscript
				</type>
				<ext>
					wmls
				</ext>
			</mime>
			<mime>
				<type>
					video/mpeg
				</type>
				<ext>
					mpeg
				</ext>
				<ext>
					mpg
				</ext>
				<ext>
					mpe
				</ext>
			</mime>
			<mime>
				<type>
					video/quicktime
				</type>
				<ext>
					qt
				</ext>
				<ext>
					mov
				</ext>
			</mime>
			<mime>
				<type>
					video/x-msvideo
				</type>
				<ext>
					avi
				</ext>
			</mime>
		</mimes>
	</server>
	<version>
		2.6.0.0
	</version>
	<console>
		<port>
			9999
		</port>
		<bindip>
			*
		</bindip>
		<ipcontrol>
			<order>
				ad
			</order>
			<allow>
				192.168.0.1-192.168.255.254
			</allow>
			<allow>
				172.16.0.1-172.31.255.254
			</allow>
			<allow>
				10.0.0.1-10.255.255.254
			</allow>
		</ipcontrol>
		<language>
			en
		</language>
		<login>
        " + user + @"
		</login>
		<password>
		</password>
	</console>
</root>
";
            #endregion
            File.WriteAllText(path + "abyss.conf", conf);
            
            //Start briefly Abyss
            Process.Start("abyssws.exe");
            Console.WriteLine("Muudan paroolid!");
            System.Threading.Thread.Sleep(1000);
            //Change the login
            try
            {
                WebClient k = new WebClient();
                k.DownloadString("http://localhost:9999/console/credentials?/console/credentials/bok=OK&/console/credentials/login=" + user + "&/console/credentials/password/$pass1=" + password + "&/console/credentials/password/$pass2=" + password);
            } catch { }

            //Change the protected dir
            try
            {
                WebRequest myReq = WebRequest.Create("http://localhost:9999/hosts/host@0/edit/ug/users/user@0/edit?/hosts/host@0/edit/ug/users/user@0/edit/bok=OK&/hosts/host@0/edit/ug/users/user@0/edit/name=" + user + "&/hosts/host@0/edit/ug/users/user@0/edit/password/$pass1=" + password + "&/hosts/host@0/edit/ug/users/user@0/edit/password/$pass2=" + password);
                string usernamePassword = user + ":" + password;
                CredentialCache mycache = new CredentialCache();
                mycache.Add(new Uri("http://localhost:9999/hosts/host@0/edit/ug/users/user@0/edit"), "Basic", new NetworkCredential(user, password));
                myReq.Credentials = mycache;
                myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));

                WebResponse wr = myReq.GetResponse();
                Stream receiveStream = wr.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
                string content = reader.ReadToEnd();
            } catch { }

            //Restart
            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName == "abyssws")
                {
                    clsProcess.Kill();
                    Console.WriteLine("Sulgen serveri protsessi " + i);
                    i++;
                }
            }
            Process.Start("abyssws.exe");
            
            //Start OWL
            Process.Start("iexplore.exe", "http://localhost/admin");            
        }

    }
}